<?php

/*
 * 中文分词类（将scws分词系统封装以方便使用，系统版权归scws作者所有）
 */
 
class Fenci extends Base{
	
	//scws对象
	protected $scws = NULL;
	
	//词库文件地址
	protected $dict = '';
	
	//规则文件地址
	protected $rule = '';
	
	//需要分词的字符串编码
	protected $charset = 'UTF-8';
	
	//字符串最大长度
	protected $max_length = 20;
	
	protected $valid = array(
		'dict',
		'rule',
		'charset',
		'max_length',
		'auto_close'
	);
	
	public function __construct($config = array())
	{
		$this->dict = APPPATH.'libraries/pscws4/etc/dict.utf8.xdb';
		$this->rule = APPPATH.'libraries/pscws4/etc/rules.utf8.ini';
		
		if ($config)
		{
			$this->config($config);
		}
	}
	
	/*
	 * 初始化
	 * @access	public
	 * @return	FALSE or $this
	 */
	public function init()
	{
		//SCWS 开源免费的简易中文分词系统，PHP分词的上乘之选！
		if (function_exists('scws_new'))
		{
			$this->scws = scws_new();
		}
		else
		{
			require_once APPPATH.'libraries/pscws4/pscws4.class.php';
			$this->scws = new PSCWS4();
		}
		
		if ( ! $this->scws)
		{
			$this->set_error('创建scws对象失败！');
			return FALSE;
		}
		elseif ( ! file_exists($this->dict))
		{
			$this->set_error('词库文件不存在！');
			return FALSE;
		}
		elseif ( ! file_exists($this->rule))
		{
			$this->set_error('规则文件不存在！');
			return FALSE;
		}
		
		$this->scws->set_charset('utf8'); //设置要分词的字符串编码
		$this->scws->set_dict($this->dict); //设置词库文件
		$this->scws->set_rule($this->rule); //设置规则集文件
		
		$this->scws->set_ignore(1); //设定分词执行过程中是否忽略所有的标点等特殊符号（不会忽略\r和\n）
		$this->scws->set_multi(1); //设定分词执行时是否执行针对长词复合切分
		$this->scws->set_duality(1); //设定是否将闲散文字自动以二字分词法聚合
		
		return $this;
	}
	
	/*
	 * 进行分词
	 * @access	public
	 * @return	array
	 */
	public function run($string)
	{
		$string = $this->change_charset($string);
		$this->scws->send_text($string);
		
		$result = array();
		
		while ($list = $this->scws->get_result())
		{
			foreach ($list as $row)
			{
				$result[] = $this->recover_charset($row['word']);
			}
		}
		
		return array_unique($result);
	}
	
	/*
	 * 关闭一个已打开的scws分词操作句柄
	 * @access	public
	 * @return	void
	 */
	public function close()
	{
		$this->scws->close();
	}
	
	/*
	 * 改变字符串字符编码为UTF-8
	 * access	protected
	 * return	string
	 */
	protected function change_charset($string)
	{
		$string = trim($string);
		
		if ( ! in_array($this->charset, array('UTF8', 'UTF-8')))
		{
			if (function_exists('mb_substr'))
			{
				$string = mb_substr($string, 0, $this->max_length, $this->charset);
			}
			elseif (function_exists('substr'))
			{
				$string = substr($string, 0, $this->max_length);
			}
			
			if (function_exists('mb_convert_encoding'))
			{
				$string = mb_convert_encoding($string, 'UTF-8', $this->charset);
			}
			elseif (function_exists('iconv'))
			{
				$string = iconv($this->charset, 'UTF-8//IGNORE', $string);
			}
		}
		
		return $string;
	}
	
	/*
	 * 恢复字符串字符编码
	 * access	protected
	 * return	string
	 */
	protected function recover_charset($string)
	{
		$string = trim($string);
		
		if ( ! in_array($this->charset, array('UTF8', 'UTF-8')))
		{
			if (function_exists('mb_convert_encoding'))
			{
				$string = mb_convert_encoding($string, 'UTF-8', $this->charset);
			}
			elseif (function_exists('iconv'))
			{
				$string = iconv($this->charset, 'UTF-8', $string);
			}
		}
		
		return $string;
	}
	
	protected function set_charset($charset)
	{
		$this->charset = in_array(strtolower($charset), array('utf-8', 'gbk', 'gb2312', 'big5')) ? strtoupper($charset) : 'UTF-8';
		return $this;
	}
	
	protected function set_dict($dict)
	{
		$this->dict = trim($dict);
		return $this;
	}
	
	protected function set_max_length($max_length)
	{
		$this->max_length = intval($max_length) > 0 ? intval($max_length) : 20;
		return $this;
	}
	
}